package com.sip.stream.utils.zfy.osd;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.Log;
import androidx.core.view.ViewCompat;
import com.blankj.utilcode.util.StringUtils;
import com.google.gson.Gson;
import com.sip.stream.ClientApp;
import com.sip.stream.R;
import com.sip.stream.utils.ByteUtils;
import com.sip.stream.utils.zfy.ZFYUserInfo;
import com.sip.stream.utils.zfy.config.ZFYOSDBeanConfig;
import com.sip.stream.utils.zfy.config.ZFYOSDConfig;
import com.sip.stream.utils.zfy.status.ZFYStatus;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;

public abstract class AbstractBaseOSDTool {
    protected static String TAG = "AbstractBaseOSDTool";

    protected static final int BYTE_WIDTH = 8;
    protected static final int LINE_HEIGHT = 16;
    protected static final int PADDING = 15;
    public static final int POSTYPE_BOTTOM_END = 3;
    public static final int POSTYPE_BOTTOM_START = 2;
    public static final int POSTYPE_TOP_END = 1;
    public static final int POSTYPE_TOP_START = 0;
    protected ZFYOSDConfig odsConfig;
    protected int all_16_32 = 16;
    protected int all_2_4 = 2;
    protected int all_32_128 = 32;
    protected int font_width = 8;
    protected int font_height = 16;
    protected int all_16 = 16;
    protected SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    protected ArrayList<OSDBean> staticOSDBeans = new ArrayList<>();
    protected ArrayList<OSDBean> dynamicOSDBeans = new ArrayList<>();
    protected int cacheScale = 1;
    protected int cacheSrcWidth = 1920;
    protected int cacheSrcHeight = 1080;
    private ZFYUserInfo userInfo = null;

    protected Context getContext() {
        return ClientApp.clientApp;
    }

    public void readConfigs(ZFYOSDConfig zFYOSDConfig) {
        this.odsConfig = zFYOSDConfig;
        setUpOSD();
    }

    private void setUpOSD() {
        this.staticOSDBeans.clear();
        this.dynamicOSDBeans.clear();
        this.userInfo = ZFYUserInfo.defaultUserInfo();
        Log.d(TAG, "cacheSrcWidth " + this.cacheSrcWidth + " cacheSrcHeight " + this.cacheSrcHeight + " cacheScale " + this.cacheScale);
        setupTrueBean(this.odsConfig.getTopStart(), 0);
        setupTrueBean(this.odsConfig.getTopEnd(), 1);
        setupTrueBean(this.odsConfig.getBottomStart(), 2);
        setupTrueBean(this.odsConfig.getBottomEnd(), 3);
    }

    private void setupTrueBean(ArrayList<ZFYOSDBeanConfig> arrayList, int i) {
        if (i == 2 || i == 3) {
            ArrayList<ZFYOSDBeanConfig> arrayList2 = new ArrayList<>();
            arrayList2.addAll(arrayList);
            Collections.reverse(arrayList2);
            arrayList = arrayList2;
        }
        Iterator<ZFYOSDBeanConfig> it = arrayList.iterator();
        int i2 = 0;
        while (it.hasNext()) {
            ZFYOSDBeanConfig next = it.next();
            OSDBean oSDBean = null;
            if (next.getOsdType() == 0) {
                oSDBean = getOneLineOSDBean(next.getInfo().replace("userName", this.userInfo.getUserName()).replace("userCode", this.userInfo.getUserCode()).replace("unitCode", this.userInfo.getUnitCode()).replace("unitName", this.userInfo.getUnitName()).replace("DeviceSN", ZFYUserInfo.defaultUserInfo().getVirtualSNCode()));
            } else if (next.getOsdType() == 2) {
                if (!StringUtils.isEmpty(next.getInfo())) {
                    oSDBean = getOneLineOSDBean(next.getInfo());
                }
            } else if (next.getOsdType() == 1) {
                oSDBean = new OSDBean();
                oSDBean.setOsdwidth(0);
                String info = next.getInfo();
                info.hashCode();
                if (info.equals("OSD_TIMESTAMP")) {
                    oSDBean.setDynamicOsdType(2);
                } else if (info.equals("OSD_STATUS")) {
                    oSDBean.setDynamicOsdType(3);
                } else {
                    oSDBean.setDynamicOsdType(1);
                }
            }
            if (oSDBean != null) {
                if (i == 1) {
                    if (oSDBean.getDynamicOsdType() == 0) {
                        oSDBean.setStartX((this.cacheSrcWidth - 15) - ((this.cacheScale * 8) * oSDBean.getOsdwidth()));
                    } else {
                        oSDBean.setStartX(-1);
                    }
                    oSDBean.setStartY((this.cacheScale * 16 * i2) + 15);
                } else if (i == 2) {
                    oSDBean.setStartX(15);
                    oSDBean.setStartY((this.cacheSrcHeight - 15) - ((this.cacheScale * 16) * (i2 + 1)));
                } else if (i == 3) {
                    if (oSDBean.getDynamicOsdType() == 0) {
                        oSDBean.setStartX((this.cacheSrcWidth - 15) - ((this.cacheScale * 8) * oSDBean.getOsdwidth()));
                    } else {
                        oSDBean.setStartX(-1);
                    }
                    oSDBean.setStartY((this.cacheSrcHeight - 15) - ((this.cacheScale * 16) * (i2 + 1)));
                } else {
                    oSDBean.setStartX(15);
                    oSDBean.setStartY((this.cacheScale * 16 * i2) + 15);
                }
                if (oSDBean.getDynamicOsdType() < 1) {
                    this.staticOSDBeans.add(oSDBean);
                } else {
                    this.dynamicOSDBeans.add(oSDBean);
                }
                i2++;
            }
        }
    }

    private OSDBean getOneLineOSDBean(String str) {
        byte[] oSDLine;
        try {
            oSDLine = getOSDLine(str);
        } catch (Exception unused) {
            oSDLine = getOSDLine("ERROR");
        }
        OSDBean oSDBean = new OSDBean();
        oSDBean.setOsdheight(16);
        oSDBean.setOsdwidth(oSDLine.length / 16);
        oSDBean.setOsdData(oSDLine);
        return oSDBean;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public int getScale(int i) {
        int i2 = (i / 16) / 40;
        if (i2 <= 0) {
            return 1;
        }
        if (i2 > 4) {
            return 4;
        }
        return i2;
    }

    public byte[] getOSDLine(String str) {
        ArrayList arrayList = new ArrayList();
        int i = 0;
        for (int i2 = 0; i2 < str.toCharArray().length; i2++) {
            String str2 = "" + str.charAt(i2);
            if (str2.equalsIgnoreCase("\n")) {
                break;
            }
            if (str2.getBytes().length > 1) {
                byte[] uTFChar = getUTFChar(str2);
                if (uTFChar != null) {
                    arrayList.add(uTFChar);
                    i += 2;
                }
            } else {
                byte[] readASCIIOSD = readASCIIOSD(str.charAt(i2));
                if (readASCIIOSD != null) {
                    arrayList.add(readASCIIOSD);
                    i++;
                }
            }
        }
        int i3 = i * 16;
        byte[] bArr = new byte[i3];
        int i4 = i3 / 16;
        Iterator it = arrayList.iterator();
        int i5 = 0;
        while (it.hasNext()) {
            byte[] bArr2 = (byte[]) it.next();
            if (bArr2.length == this.all_32_128) {
                for (int i6 = 0; i6 < 16; i6++) {
                    int i7 = (i6 * i4) + i5;
                    int i8 = i6 * 2;
                    bArr[i7] = bArr2[i8];
                    bArr[i7 + 1] = bArr2[i8 + 1];
                }
                i5 += 2;
            } else {
                for (int i9 = 0; i9 < 16; i9++) {
                    bArr[(i9 * i4) + i5] = bArr2[i9];
                }
                i5++;
            }
        }
        return bArr;
    }

    private byte[] readASCIIOSD(char c) {
        int i = c * 16;
        byte[] bArr = new byte[this.all_16];
        try {
            InputStream openRawResource = getContext().getResources().openRawResource(R.raw.asc16);
            openRawResource.skip(i);
            openRawResource.read(bArr, 0, this.all_16);
            openRawResource.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bArr;
    }

    private byte[] readChinaOSD(String str) {
        int i = 0;
        byte[] bArr = null;
        try {
            byte[] bytes = str.getBytes("gbk");
            if (bytes.length == 2) {
                int i2 = bytes[0];
                if (i2 < 0) {
                    i2 += 256;
                }
                int i3 = bytes[1];
                if (i3 < 0) {
                    i3 += 256;
                }
                int i4 = i2 - 160;
                int i5 = i3 - 160;
                try {
                    InputStream openRawResource = getContext().getResources().openRawResource(R.raw.hzk16h);
                    int i6 = ((((i4 - 1) * 94) + i5) - 1) * this.all_32_128;
                    long skip = openRawResource.skip(i6);
                    bArr = new byte[this.all_32_128];
                    Log.i(TAG, str + " offset " + i6 + " skipRet " + skip + " readRet " + openRawResource.read(bArr, 0, i));
                    openRawResource.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.w(TAG, "" + e.getMessage());
                }
                Log.w(TAG, "readChinaOSD " + str + " " + ByteUtils.bytesToHexString(bArr));
                return bArr;
            }
            Log.d(TAG, " osd_gbk.length != 2");
            return getUTFChar(str);
        } catch (UnsupportedEncodingException e2) {
            e2.printStackTrace();
            return null;
        }
    }

    public void refreshOSDInfo() {
        Log.i(TAG, "refreshOSDInfo");
        setUpOSD();
    }


    public String getGPSStr() {
        double latitude = ZFYStatus.getInstance().getLatitude();
        double longitude = ZFYStatus.getInstance().getLongitude();
        return String.format("%s%.6f %s%.6f", latitude >= 0.0d ? "N:" : "S:", Double.valueOf(latitude), longitude >= 0.0d ? "E:" : "W:", Double.valueOf(longitude));
    }

    public String getTimeStampStr() {
        return this.dateFormat.format(new Date());
    }

    public String getDeviceStatusStr() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("Bat:%d%% Stg:%.2fG ", Integer.valueOf(ZFYStatus.getInstance().getBatteryLevel()), Float.valueOf(ZFYStatus.getInstance().getEmptyGbSize())));
        if (ZFYStatus.getInstance().getAirplaneState() > 0) {
            sb.append("Air");
        } else if (ZFYStatus.getInstance().getWifiSignalLevel() >= 0) {
            sb.append("Wi-Fi");
        } else {
            sb.append(ZFYStatus.getInstance().getMobileNetworkStr());
        }
        return sb.toString();
    }


    public byte[] getUTFChar(String str) {
        int[] iArr = new int[256];
        Bitmap renderTextToBitmap = renderTextToBitmap(str, 16, 16);
        renderTextToBitmap.getPixels(iArr, 0, 16, 0, 0, 16, 16);
        renderTextToBitmap.recycle();
        int i = this.all_32_128;
        byte[] bArr = new byte[i];
        for (int i2 = 0; i2 < i; i2++) {
            int i3 = i2 / 2;
            int i4 = i2 % 2;
            byte b = 0;
            for (int i5 = 0; i5 < 8; i5++) {
                if (iArr[(i3 * 16) + (8 * i4) + i5] != -1) {
                    b = (byte) (b | (1 << (7 - i5)));
                }
            }
            bArr[i2] = b;
        }
        return bArr;
    }

    public static Bitmap renderTextToBitmap(String str, int i, int i2) {
        Bitmap createBitmap = Bitmap.createBitmap(i, i2, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        canvas.drawColor(-1);
        Paint paint = new Paint();
        paint.setColor(ViewCompat.MEASURED_STATE_MASK);
        paint.setTextSize(16.0f);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTypeface(Typeface.MONOSPACE);
        paint.getTextBounds(str, 0, str.length(), new Rect());
        canvas.drawText(str, i / 2, 14, paint);
        return createBitmap;
    }
}
